package cn.netty.exemple6.marshall;

import io.netty.handler.codec.marshalling.*;
import org.jboss.marshalling.MarshallerFactory;
import org.jboss.marshalling.Marshalling;
import org.jboss.marshalling.MarshallingConfiguration;

/**
 *
 * marshalling 的编解码器自带解决粘包的问题
 * <p>Title: MarshallCodeFactory</p>
 * <p>Description:Marshall编解码工厂</p>
 * @version V1.0
 * @author ZhiPeng_Chen
 * @date: 2018/11/2
 */
public final class MarshallCodeFactory {

    /**
     * 使用jboss的Marshalling解码器
     * serial 是创建java的序列化工厂对象
     * configuration.setVersion(5); 是设置jdk的版本号
     * @return
     */
    public static MarshallingDecoder deCoder(){

        final MarshallerFactory factory = Marshalling
                .getProvidedMarshallerFactory("serial");

        final MarshallingConfiguration configuration = new MarshallingConfiguration();
        configuration.setVersion(5);

         UnmarshallerProvider provider = new DefaultUnmarshallerProvider(factory,configuration);
        /**
         * maxObjectSize 设置最大的对象
         */
        MarshallingDecoder decoder = new MarshallingDecoder(provider, 1024 * 1024 *1);

        return decoder;

    }

    /**
     * 使用jboss的Marshalling编码器
     * @return
     */
    public static MarshallingEncoder enCoder(){

        final MarshallerFactory factory = Marshalling
                .getProvidedMarshallerFactory("serial");

        final MarshallingConfiguration configuration = new MarshallingConfiguration();
        configuration.setVersion(5);

        MarshallerProvider provider = new DefaultMarshallerProvider(factory,configuration);
        MarshallingEncoder encoder = new MarshallingEncoder(provider);

        return encoder;

    }
}
